package com.mapapp.flutter_mapapp.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.ProcessBean;

import java.text.DecimalFormat;
import java.util.List;

public class StepAdapter extends BaseQuickAdapter<ProcessBean, BaseViewHolder> {

    public StepAdapter(int layoutResId, @Nullable List<ProcessBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ProcessBean item) {
        TextView tvStep = helper.getView(R.id.tv_step);
        TextView tvPercentage = helper.getView(R.id.tv_percentage);

        DecimalFormat df = new DecimalFormat("0.00");
        helper.setText(R.id.tv_step, item.getProcessName())
                .setText(R.id.tv_percentage, (
                        item.getProcessPercent() == 0 ? 0 : item.getProcessPercent() == 1 ? 100 : df.format(item.getProcessPercent() * 100)) + "%");

        ProgressBar progressBar = helper.getView(R.id.progress_bar);
        progressBar.setProgress((int) (item.getProcessPercent() * 100));

        int position = getData().indexOf(item);
        switch (position) {
            case 0:
                setColor(Color.parseColor("#FAE99A"), tvStep, tvPercentage);
                progressBar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.step1_progress_bar));
                break;
            case 1:
                setColor(Color.parseColor("#DDA9AE"), tvStep, tvPercentage);
                progressBar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.step2_progress_bar));
                break;
            case 2:
                setColor(Color.parseColor("#9BC3BB"), tvStep, tvPercentage);
                progressBar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.step3_progress_bar));
                break;
            case 3:
                setColor(Color.parseColor("#82779F"), tvStep, tvPercentage);
                progressBar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.step4_progress_bar));
                break;
            case 4:
                setColor(Color.parseColor("#8EA4D3"), tvStep, tvPercentage);
                progressBar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.step5_progress_bar));
                break;
        }

        ViewGroup.LayoutParams params = helper.itemView.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth() / 4;
        helper.itemView.setLayoutParams(params);
    }

    private void setColor(int color, TextView... textView) {
        for (TextView view : textView) {
            view.setTextColor(color);
        }
    }
}
